package cdc.cs.controller;

import cdc.cs.common.JSONResult;
import cdc.cs.entity.BusinessPartnersVO;
import cdc.cs.service.ODataPartnerService;
import cdc.cs.service.TimerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @ClassName: PartnerController
 * @Description: TODO
 * @Author: kevin
 * @Date: 2020/5/28 15:19
 * @Version: 1.0
 */
@Api(value = "Partner中心", tags = {"Partner模块相关接口"})
@RestController
@RequestMapping("partner")
public class PartnerController {

    @Autowired
    ODataPartnerService partnerService;
    @Autowired
    TimerService timerService;

    @GetMapping("/total")
    @ApiOperation(value = "统计合作伙伴总数", notes = "统计合作伙伴总数", httpMethod = "GET")
    public JSONResult getAll(){
        Integer count = partnerService.partnerGetAll();
        if (StringUtils.isEmpty(count)) {
            return JSONResult.errorMsg("统计合作伙伴总数获取失败");
        } else {
            return JSONResult.ok(count);
        }
    }
    @GetMapping("/pagination")
    @ApiOperation(value = "统计合作伙伴分页查询", notes = "统计合作伙伴分页查询", httpMethod = "GET")
    public JSONResult getone(
            @ApiParam(name = "num", value = "页数", required = true)
            @RequestParam Integer num) {
        List<BusinessPartnersVO> list = partnerService.partnerPagination(num);
        if (list != null && list.size() > 0) {
            return JSONResult.ok(list);
        } else {
            return JSONResult.errorMsg("合作伙伴分页获取失败");
        }
    }

    @GetMapping("/updateAll")
    @ApiOperation(value = "更新数据", notes = "更新数据", httpMethod = "GET")
    public JSONResult updateAll() {
        try {
            timerService.updatePartnerData();
            return JSONResult.ok();
        } catch (Exception e) {
            return JSONResult.errorException(e.getMessage());
        }

    }
}
